package com.android.bandroid.api.util;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

/**
 * Volley 单例类
 * Created by Kevin on 2015/11/6.
 */
public class VolleySingleton {
    private static VolleySingleton volleySingleton;
    private RequestQueue mRequestQueue;
    private Context mContext;

    public VolleySingleton(Context context){
        this.mContext = context;
        mRequestQueue = getRequestQueue();
    }

    public static synchronized  VolleySingleton getVolleySingleton(Context context){
        if(volleySingleton == null){
            volleySingleton = new VolleySingleton(context);
        }
        return volleySingleton;
    }

    /**
     * 获得请求队列
     * @return
     */
    public RequestQueue getRequestQueue(){
        if(mRequestQueue == null){
            mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext());
        }
        return mRequestQueue;
    }

    /**
     * 将请求放入请求队列中
     * @param request
     * @param <T>
     */
    public <T> void addToRequestQueue(Request<T> request){
        getRequestQueue().add(request);
    }
}
